<div class="headbar">
	<div class="position"><span>插件管理</span><span>></span><span>插件</span><span>></span><span>微信公众账号菜单</span></div>
</div>
<div class="content_box">
	<div class="content form_content">
		<form action="#" method="post" name="wechat">
			<table class="form_table">
				<colgroup>
					<col width="150px" />
					<col />
				</colgroup>
				<tr>
					<th>微商城帮助：</th>
					<td>
						<a href='http://www.aircheng.com/news/74-wechat' target='_blank'>
							{if:extension_loaded("OpenSSL")}不知道如何配置？{else:}系统没有开启OpenSSL组件无法使用此功能{/if}
						</a>
					</td>
				</tr>

				<tr>
					<th>URL(服务器地址)：</th>
					<td>
						{if:IWeb::$app->config['rewriteRule'] == 'pathinfo'}
						<span class="orange">{echo:IUrl::getHost()}{url:/block/wechat}</span>
						<label>复制到<微信公众平台后台-开发者中心>URL（服务器地址）</label>
						{else:}
						<span class="red">必须开启伪静态 【系统】——【网站设置】——【系统设置】——【开启伪静态】</span>
						{/if}
					</td>
				</tr>
				<tr>
					<th>公众平台菜单：</th>
					<td>
						<table class="list_table" style="width:700px">
							<colgroup>
								<col />
								<col width="350px" />
								<col width="80px" />
							</colgroup>

							<thead>
								<tr>
									<th>菜单名称</th>
									<th>连接地址</th>
									<th>操作</th>
								</tr>
							</thead>

							<tbody id="menuBox"></tbody>

							<tfoot>
								<tr><td colspan="3" style="text-align:left"><button type='button' class='btn' onclick='addMainMenu([]);'><span>添加菜单</span></button></td></tr>
							</tfoot>
						</table>

						<p><label>微信公众账号菜单设置，<连接地址> 填写完整的URL路径，比如： http://shop.aircheng.com </label></p>
					</td>
				</tr>

				<tr><td></td><td><button class="submit" type="button" onclick="saveMenu();"><span>保 存</span></button></td></tr>
			</table>
		</form>
	</div>
</div>

<!--菜单项模板-->
<script type='text/html' id='menuTemplate'>
<%if(isChild == true){%>
<tr name="child<%=menuIndex%>">
	<td><input type='text' class='small' name='menuChildName<%=menuIndex%>' value='<%=menuData.name%>' style='margin-left:50px' /></td>
	<td><input type='text' class='normal' name='menuChildLink<%=menuIndex%>' value='<%=menuData.url%>' alt='<%=menuData.url%>' title='<%=menuData.url%>' /></td>
	<td><a href="javascript:void(0);" onclick="delChildMenu(this);"><img class="operator" src="{skin:images/admin/icon_del.gif}" alt="删除" title="删除" /></a></td>
</tr>
<%}else{%>
<tr name="parent<%=menuIndex%>">
	<td><input type='text' class='small' name='menuName' value='<%=menuData.name%>' menuIndex="<%=menuIndex%>" /></td>
	<td><input type='text' class='normal' name='menuLink' value='<%=menuData.url%>' alt='<%=menuData.url%>' title='<%=menuData.url%>' /></td>
	<td>
		<a href="javascript:addChildMenu(<%=menuIndex%>,[]);"><img class="operator" src="{skin:images/admin/icon_add.gif}" alt="添加子菜单" title="添加子菜单" /></a>
		<a href="javascript:delParentMenu(<%=menuIndex%>);"><img class="operator" src="{skin:images/admin/icon_del.gif}" alt="删除" title="删除" /></a>
	</td>
</tr>
<%}%>
</script>

<script type='text/javascript'>
jQuery(function()
{
	//获取菜单信息
	$.getJSON('{url:/plugins/getWechatMenu}',{"rand":Math.random()},function(json)
	{
		if(json.result == 'success')
		{
			var menuData = jQuery.parseJSON(json.data);
			if(menuData.button)
			{
				for(var index in menuData.button)
				{
					var item = menuData.button[index];
					addMainMenu(item);
					if(item.sub_button)
					{
						for(var i in item.sub_button)
						{
							var itemSec = item.sub_button[i];
							addChildMenu(index,itemSec);
						}
					}

				}
			}
		}
		else if(json.errmsg)
		{
			alert(json.errmsg);
		}
		else if(json.msg)
		{
			alert(json.msg);
		}
	});
});

//保存菜单
function saveMenu()
{
	var menuNum = $('tr[name^="parent"]').length;
	var buttonObject = {"button":[]};

	for(var i=0;i<menuNum;i++)
	{
		var tempName = $("input[name='menuName']:eq("+i+")").val();
		if(!tempName)
		{
			continue;
		}
		var tempLink = $("input[name='menuLink']:eq("+i+")").val();
		var item     = {"type":"view","name":tempName};

		//存在子菜单
		var menuIndex = $("input[name='menuName']:eq("+i+")").attr("menuIndex");
		var childNum = $("tr[name='child"+menuIndex+"']").length;
		if(childNum > 0)
		{
			item.sub_button = [];
			for(var j=0;j<childNum;j++)
			{
				var tempChildName = $("input[name='menuChildName"+menuIndex+"']:eq("+j+")").val();
				if(!tempChildName)
				{
					continue;
				}
				var tempChildLink = $("input[name='menuChildLink"+menuIndex+"']:eq("+j+")").val();
				var tempItem      = {"type":"view","name":tempChildName,"url":tempChildLink};
				item.sub_button.push(tempItem);
			}
		}
		else
		{
			item.url = tempLink;
		}
		buttonObject.button.push(item);
	}

	$.post("{url:/plugins/setWechatMenu}",{"menuData":JSON.stringify(buttonObject)},function(json)
	{
		if(json.result == 'success')
		{
			alert('菜单修改成功');
		}
		else if(json.errmsg)
		{
			alert(json.errmsg);
		}
		else if(json.msg)
		{
			alert(json.msg);
		}
	},'json');
}

/**
 * 添加菜单
 * @param menuData {"type":"view","name":"微商城","url":"连接地址","sub_button":[]}
 */
function addMainMenu(menuData)
{
	var parentMenuLength = $("tr[name^='parent']").length;
	if(parentMenuLength >= 3)
	{
		alert("菜单数量最多不能超过3个");
		return;
	}
	var menuHtml = template.render('menuTemplate',{"isChild":false,"menuIndex":parentMenuLength,"menuData":menuData});
	$('#menuBox').append(menuHtml);
}

/**
 * 添加二级菜单
 * @param menuIndex 菜单索引值
 * @param menuData {"type":"view","name":"微商城","url":"连接地址","sub_button":[]}
 */
function addChildMenu(menuIndex,menuData)
{
	var childMenuLength = $("tr[name='child"+menuIndex+"']").length;
	if(childMenuLength >= 5)
	{
		alert("子菜单的数量不能超过5个");
		return;
	}
	var parentObj = $("tr[name='parent"+menuIndex+"']");
	var menuHtml  = template.render('menuTemplate',{"isChild":true,"menuIndex":menuIndex,"menuData":menuData});
	parentObj.after(menuHtml);
}

//删除主菜单
function delParentMenu(menuIndex)
{
	$("tr[name='parent"+menuIndex+"']").remove();
	$("tr[name='child"+menuIndex+"']").remove();
}

//删除子菜单
function delChildMenu(selfObj)
{
	$(selfObj).parent().parent().remove();
}
</script>